{$contents}

{literal}
<div style="text-align:left; padding:10px;">







<h2>InpItem class</h2>
<pre class="code">
&lt;?php
/**
 * @orm tableName inpitems
 */
class InpItem extends DomArObject {
	
	/**
	 * @orm text
	 */
	public $title;
	/**
	 * @orm has many InpCategory inverse items
	 */
	public $categories;
	
}
?&gt;
</pre>







<h2>InpCategory class</h2>
<pre class="code">
&lt;?php
/**
 * @orm tableName inpcategories
 */
class InpCategory extends DomArObject {
	
	/**
	 * @orm text
	 */
	public $name;
	/**
	 * @orm has parents InpItem inverse categories
	 */
	public $items;
	
	
}
?&gt;
</pre>







<h2>Source</h2>
<pre class="code">
&lt;?php
$item = $id ? InpItem::load($id) : new InpItem();
		
$this->view->addPartitioner('contents', 'DomFg/Source/InputListReal.html');

$post = DomFgPost::getInstance();

if( $post->save ) {
	
	$item->title = $post->title;
	
	
	if( $post->whichForm == 1 ) {
		$categoryValues = $post->categories_check;
	} else if( $post->whichForm == 2) {
		$categoryValues = $post->categories_multi;
	} else if( $post->whichForm == 3) {
		$categoryValues = array_keys($post->categories_checkGroup);
	}
	
	// set method takes ID or objects as input and sets all children/parents to these. 
	// meaning that every parent/child that is not in given list is removed
	// and new parents/children are added.
	$item->categories->set( $categoryValues );


	if( $item->save() ) {
		$id = $item->id;
		$message = array(
			'type' => 'ok',
			'message' => 'Saved',
			'colspan' => 2,
		);
	} else {
		$message = array(
			'type' => 'error',
			'message' => $item->getValidationMessages('<br>'),
			'colspan' => 2,
		);
	}
}

$contents = array(
	'type' => 'panel',
	'title' => 'DomFg input list working sample with DomAr',
	'style' => 'margin: 10px; width: 800px;',
	'layout' => 'table',
	'action' => $this->getUrl('inputListReal/'.$id),
	'layoutConfig' => array(
		'columns' => 2,
		'columnWidths' => array(150,'*'),
			),
			'items' => array(
				$message,
				( $item->id == 0 ? NULL : array(
					'type' => 'tip',
					'message' => 'Changing element with id '.$item->id,
					'colspan' => 2,
				)),
				array(
					'type' => 'textField',
					'title' => 'Title',
					'value' => $item->title,
					'name' => 'title',
				),
				array(
					'type' => 'radioGroup',
					'title' => 'Which form to use',
					'values' => array( 1 => 'Checkboxes', 2 => 'MultiSelect', 3 => 'Checkbox group'),
					'checked' => $post->whichForm ? $post->whichForm : 1,
					'name' => 'whichForm',
				),
			),
			'buttons' => array(
		array(
			'name' => 'save',
			'title' => 'Save',
		),
	),
);

// SINGLE CHECKBOXES
$contents['items'][] = array(
	'type' => 'separator',
	'title' => 'Single checkboxes',
	'colspan' => 2,
);
$selectedCategories = $item->categories->idSetQuery('id')->commit()->toArray();
foreach( InpCategory::idSetQuery('name') as $id => $name ) {
	$contents['items'][] = array(
		'title' => $name,
		'type' => 'checkBox',
		'name' => 'categories_check[]',
		'value' => $id,
		'checked' => (bool)$selectedCategories[$id],
	);
}

// MULTI SELECT
// same thing can be done with multiSelect
$contents['items'][] = array(
	'type' => 'separator',
	'title' => 'Multi select',
	'colspan' => 2,
);
$contents['items'][] = array(
	'type' => 'multiSelect',
	'title' => 'Categories',
	'name' => 'categories_multi',
	'values' => InpCategory::idSetQuery('name')->commit()->toArray(),
	'selected' => arrayFlipTrue( $item->categories->idSetQuery('id')->commit()->toArray() ),
);


// CHECKBOX GROUP
// same thing can be done with checkBoxGroup
$contents['items'][] = array(
	'type' => 'separator',
	'title' => 'Checkbox group',
	'colspan' => 2,
);
$contents['items'][] = array(
	'type' => 'checkBoxGroup',
	'title' => 'Categories',
	'name' => 'categories_checkGroup',
	'values' => InpCategory::idSetQuery('name')->commit()->toArray(),
	'selected' => arrayFlipTrue( $item->categories->idSetQuery('id')->commit()->toArray() ),
);

$this->view->contents = DomFg::render( $contents );
?&gt;
</pre>
</div>
{/literal}